package com.myjob.controller;

import com.myjob.config.Constants;
import com.myjob.entity.Blog;
import com.myjob.exception.OptionException;
import com.myjob.response.Result;
import com.myjob.service.BlogService;
import com.myjob.utils.UtilValidate;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * @author Chen Yuan
 * @create 2021-01-21  19:07
 */
@RestController
@Slf4j
@RequestMapping("/blog")
public class BlogController {
    @Autowired
    private BlogService blogService;

    /**
     * 查询 博客信息
     *
     * @param userName    用户名
     * @param blogTitle   博客名
     * @param deleteFlag  是否逻辑删除
     * @param currentPage 当前页
     * @param pageSize    每页大小
     */
    @GetMapping("/blogInfo")
    public Result selectBlog(@RequestParam(value = "currentPage") Integer currentPage,
                             @RequestParam(value = "searchValue", required = false) String searchValue,
                             @RequestParam(value = "userId", required = false) String userId,
                             @RequestParam(value = "userName", required = false) String userName,
                             @RequestParam(value = "blogTitle", required = false) String blogTitle,
                             @RequestParam(value = "deleteFlag", required = false) String deleteFlag,
                             @RequestParam(value = "pageSize", required = false) Integer pageSize) {
        Result result = null;
        try {

            Integer deleted = Constants.getNoDel();
            if (UtilValidate.isNotEmpty(deleteFlag) &&
                    deleteFlag.equals("yet_del")) {
                deleted = Constants.getYetDel();
            }
            result = blogService.selectBlog(searchValue, userId, userName, blogTitle, deleted, currentPage, pageSize);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 查询热点博客
     *
     * @param number 查询阅读量排在前几的
     * @return
     */
    @GetMapping("/hotBlog")
    public Result selectHotBlog(@RequestParam(value = "number", required = false) String number) {
        Result result = null;
        try {
            result = blogService.selectHotBlog(number);
        } catch (Exception e) {
            e.printStackTrace();
            throw new OptionException("查询热点博客出错");
        }
        return result;
    }

    @GetMapping("/oneBlog")
    public Result selectBlogById(@RequestParam("blogId") Long blogId){
        Result result = null;
        try {
            result = blogService.selectBlogById(blogId);
        } catch (Exception e) {
            e.printStackTrace();
            throw new OptionException("查询失败");
        }
        return result;
    }
    /**
     * 添加博客
     *
     * @param blog 博客内容
     */
    @PutMapping("/blogInfo")
    public Result addBlog(@RequestBody Blog blog) {
        Result result = null;
        try {
            // 插入默认信息
            blog.setDeleted(0);
            blog.setBlogId(null);
            int insert = blogService.addBlog(blog);
            if (insert == 1) {
                result = Result.succeed("博客 添加成功");
            } else {
                result = Result.failed("博客 添加失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 修改博客
     *
     * @param blog 博客内容
     * @return 影响行数
     */
    @PostMapping("/blogInfo")
    public Result updateBlog(@RequestBody Blog blog) {
        Result result = null;
        try {
            int update = blogService.updateBlog(blog);
            if (update == 1) {
                result = Result.succeed("博客 修改成功");
            } else {
                result = Result.failed("博客 修改失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 删除博客(物理删除)
     *
     * @param blogId 博客编号
     * @return 封装的结果
     */
    @DeleteMapping("/blogInfo")
    public Result deleteBlog(@RequestParam("blogId") Long blogId) {
        Result result = null;
        try {
            int delete = blogService.deleteBlog(blogId);
            if (delete == 10) {
                result = Result.succeed("博客 放入回收站");
            } else if (delete == 20) {
                result = Result.succeed("博客 删除成功");
            } else {
                result = Result.failed("博客 删除失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
}
